﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BugzX.Models;
using BugzX.Repositories;
using DotNetX;
using BugzX;

namespace BugzX.Controllers
{
    public class BugController : Controller
    {
        //
        // GET: /Bug/

        [Authorize()]
        public ActionResult New(string name)
        {
            var vd = new BugNewViewData();

            vd.ProjectName = name;
            vd.Components = ProjectRepository.GetComponentsByProjectName(name);

            return this.View(vd);
        }

        [Authorize()]
        [HttpPost()]
        public ActionResult New(BugNewViewData vd)
        {
            FormChecker checker = new FormChecker();

            Bug b = new Bug();

            b.Title = checker.GetString(vd.Title, "Title");
            b.BugType = vd.Type;
            b.Priority = vd.Priority;
            b.ProjectComponentId = vd.ComponentId;
            b.Stamp = DateTime.Now;
            b.OwnerId = UserRepository.GetCurrentUserId();
            b.Status = (int)BugStatus.Open;

            BugNote note = new BugNote();

            note.Notes = checker.GetString(vd.Description, "Description");

            if (checker.HasErrors)
            {
                vd.ErrorMessage = checker.ErrorsByBRTag;

                vd.Components = ProjectRepository.GetComponentsByProjectName(vd.ProjectName);

                return this.View(vd);
            }

            var context = new BugzXDBEntities();

            context.Bugs.AddObject(b);
            context.SaveChanges();

            note.BugId = b.BugId;
            note.Stamp = DateTime.Now;
            note.OwnerId = UserRepository.GetCurrentUserId();

            context.BugNotes.AddObject(note);
            context.SaveChanges();

            if (!String.IsNullOrEmpty(vd.DependentOn))
            {
                string dep = vd.DependentOn.Trim();

                string[] d = dep.Split(',');

                int added = 0;
                foreach (string id in d)
                {
                    if (ConvertExtensions.IsInt32(id))
                    {
                        BugDependency bd = new BugDependency();
                        bd.ParentId = Convert.ToInt32(id);
                        bd.BugId = b.BugId;

                        context.BugDependencies.AddObject(bd);

                        added++;
                    }
                }

                if(added > 0)
                    context.SaveChanges();
            }

            if (!String.IsNullOrEmpty(vd.ParentTo))
            {
                string par = vd.ParentTo.Trim();
                string[] p = par.Split(',');

                int added = 0;
                foreach (string id in p)
                {
                    if (ConvertExtensions.IsInt32(id))
                    {
                        BugDependency bd = new BugDependency();
                        bd.ParentId = b.BugId;
                        bd.BugId = Convert.ToInt32(id);

                        context.BugDependencies.AddObject(bd);

                        added++;
                    }
                }

                if (added > 0)
                    context.SaveChanges();
            }

            ProjectViewViewData pvd = new ProjectViewViewData();
            pvd.Name = vd.ProjectName;
            pvd.AddedBugId = b.BugId;

            return RedirectToAction("view", "project", pvd);
        }
    }
}
